package com.baize.filedowndemo.workmanager

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.work.*
import androidx.work.impl.utils.futures.SettableFuture
import com.baize.filedowndemo.MainActivity
import com.baize.filedowndemo.R
import com.baize.filedowndemo.conversationFile.ProgressInterceptor
import com.google.common.util.concurrent.ListenableFuture
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit

class TestWorker(val context: Context, workerParams: WorkerParameters) :
  Worker(context, workerParams) {

  @SuppressLint("RestrictedApi")
  override fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo> {
    val notification = NotificationCompat.Builder(context, "some-channel-id").setContentIntent(
        PendingIntent.getActivity(
          context,
          0,
          Intent(context, MainActivity::class.java),
          PendingIntent.FLAG_UPDATE_CURRENT
        )
      )
      .setSmallIcon(R.drawable.ic_launcher_foreground)
      .setOngoing(true).setAutoCancel(true)
      .setOnlyAlertOnce(true).setPriority(NotificationCompat.PRIORITY_MIN)
      .setContentTitle("FileDownDemoTitle").setLocalOnly(true)
      .setVisibility(NotificationCompat.VISIBILITY_SECRET).setContentText("Updating widget").build()
    val future : SettableFuture<ForegroundInfo> = SettableFuture.create()
    future.set(ForegroundInfo(1337, notification))
    return future
  }

  override fun doWork(): Result {
//    val firstUpdate = workDataOf("Progress" to 0)
//    val lastUpdate = workDataOf("Progress" to 100)
    repeat(20) {
      setProgressAsync(workDataOf("Progress" to (it * 5)))
      Log.i("baize_", "task running...$it")
      Thread.sleep(1000)
    }
    return Result.success()
  }
}